package com.house.keeper.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.house.keeper.base.BaseController;
import com.house.keeper.base.Result;
import com.house.keeper.base.ResultCode;
import com.house.keeper.constant.ConstantConfig;
import com.house.keeper.entity.User;
import com.house.keeper.entity.bean.Service;
import com.house.keeper.entity.vo.LoginVo;
import com.house.keeper.entity.vo.UpdatePwdVo;
import com.house.keeper.redis.RedisTemplateUtil;
import com.house.keeper.service.MenuService;
import com.house.keeper.service.UserService;
import com.house.keeper.shiro.util.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


/**
 * 登录相关信息
 * @author zjj
 * @date 2022-05-02
 */
@Controller
@Slf4j
@Api(tags = "登录")
public class LoginController extends BaseController {
    @Autowired //标记需要注入的依赖项
    private UserService userService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RedisTemplateUtil redisTemplateUtil;

    /**
     * 跳转到登录页面
     * @return
     */

    @ApiOperation("跳转到登录界面")
    @GetMapping(value = "/login")
    public String login(){
        return "login";
    }

    /**
     * 登录判断
     * @param user
     * @return
     */
    @ApiOperation("登录成功或者失败")
    @PostMapping(value = "/index")
    public ModelAndView index(LoginVo user){
        ModelAndView modelAndView = new ModelAndView();
//        登录校验
        Result result =userService.checkLogin(user);

        if(result.getResultCode()==ResultCode.SUCCESS){

            User us = (User)result.getData();
            log.info("登录用户信息："+JSONArray.toJSONString(us));
            redisTemplateUtil.set(ConstantConfig.USER_INFO, JSONArray.toJSONString(us));

            modelAndView.addObject("menusList",menuService.queryMenuByUserId(us.getId()));
            modelAndView.addObject("nickName",us.getNickName());
            modelAndView.setViewName("index");
        }else{
            modelAndView.addObject("msg",result.getResultCode().showMsg());
            modelAndView.setViewName("login");
        }

        return modelAndView;
    }

    /**
     * 退出
     * @return
     */
    @ApiOperation("退出")
    @GetMapping(value = "/logout")
    public ModelAndView logout(){
        ModelAndView modelAndView = new ModelAndView();
        ShiroUtils.logout();
        redisTemplateUtil.delete(ConstantConfig.USER_INFO);
        modelAndView.setViewName("login");
        return modelAndView;
    }

    /**
     * 跳转到首页
     * @return
     */
    @ApiOperation("跳转到首页")
    @GetMapping(value = "/firstPage")
    public ModelAndView index_x(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("systemService",new Service());
        modelAndView.setViewName("index-x");
        return modelAndView;
    }

    /**
     * 跳转到修改密码页面
     * @return
     */
    @ApiOperation("跳转到修改密码界面")
    @GetMapping(value = "/toUpdatePwd")
    @RequiresPermissions("system:user:edit")
    public ModelAndView toUpdatePwd(){
        ModelAndView modelAndView = new ModelAndView();

        User us=JSONArray.parseObject(redisTemplateUtil.get(ConstantConfig.USER_INFO).toString(),User.class);

        modelAndView.addObject("oldPwd",us.getUserPwd());
        modelAndView.setViewName("user/updatePwd");
        return modelAndView;
    }
    @ApiOperation("修改密码")
    @PostMapping(value = "/updatePwd")
    @RequiresPermissions("system:user:edit")
    @ResponseBody
    public JSONObject updatePwd(@RequestBody UpdatePwdVo updatePwdVo){
        Result result = userService.updatePwd(updatePwdVo);
        return responseJSONObject(result);
    }

}
